package com.service.serviceImpl;

import com.mapper.ProductMapper;
import com.service.IProductService;
import com.vo.productVo.ProductListRequestVo;
import com.vo.productVo.ProductListResponseVo;
import com.vo.productVo.addProductVo;
import com.vo.productVo.updateProductVo;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author hl
 * @Date
 * @Describe 商品相关功能实现
 */
@Service
public class ProductServiceImpl implements IProductService {

    Logger logger = LoggerFactory.getLogger(IProductService.class);

    @Resource
    ProductMapper productMapper;

    /**
     * 获取商品信息列表
     * @param vo
     * @return
     */
    @Override
    public List<ProductListResponseVo> getProductList(ProductListRequestVo vo) {
        return productMapper.getProductList(vo);
    }

    /**
     * 修改商品信息
     * @param vo
     */
    @Override
    public void updateProduct(updateProductVo vo) {
        ProductListResponseVo product = productMapper.getProductById(vo.getId());
        if(!vo.getName().equals(product.getName()) ||
                !vo.getDescribe().equals(product.getDescribe()) ||
                !vo.getPurchasingPrice().equals(product.getPurchasingPrice()) ||
                !vo.getSellingPrice().equals(product.getSellingPrice()) ||
                !vo.getStock().equals(product.getStock())) {
            vo.setUpdateTime(LocalDateTime.now());
            productMapper.updateProduct(vo);
        }
    }

    /**
     * 上架已有商品
     * @param id
     */
    @Override
    public void putAwayProduct(Integer id) {
        ProductListResponseVo product = productMapper.getProductById(id);
        LocalDateTime updateTime = LocalDateTime.now();
        if(product.getStatus().equals("下架")){
            productMapper.putAwayProduct(id,updateTime);
        }
    }

    /**
     * 下架商品
     * @param id
     */
    @Override
    public void soldOutProduct(Integer id) {
        ProductListResponseVo product = productMapper.getProductById(id);
        LocalDateTime updateTime = LocalDateTime.now();
        if(product.getStatus().equals("在售")) {
            productMapper.soldOutProduct(id, updateTime);
        }
    }

    /**
     * 上架新商品
     * @param addVo
     */
    @Override
    public void addProduct(addProductVo addVo) {
//        遍历判断该商品是否存在，若不存在，则做添加操作
        ProductListRequestVo vo = new ProductListRequestVo();
        List<ProductListResponseVo> list = productMapper.getProductList(vo);
        List<String> nameList = new ArrayList<>();
        List<String> describeList = new ArrayList<>();
        for(int i = 0; i < list.size(); i++){
            nameList.add(list.get(i).getName());
            describeList.add(list.get(i).getDescribe());
        }
        if(!nameList.contains(addVo.getName()) || !describeList.contains(addVo.getDescribe())){
            addVo.setCreateTime(LocalDateTime.now());
            productMapper.addProduct(addVo);
        }
        if(nameList.contains(addVo.getName()) && describeList.contains(addVo.getDescribe())){
            //此条只会在控制台显示
//            System.out.println("此商品已在库中！");
        }
    }
}
